/**
 * Copyright (C) 2019 Baidu, Inc. All Rights Reserved.
 */
package com.bailiban.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.bailiban.dao.UserDao;
import com.bailiban.pojo.User;
import com.bailiban.utils.JDBCUtils;
import com.bailiban.utils.ResultMsg;

/**
 * @author zhulang
 * @version 创建时间：2019年9月27日 上午10:33:31
 */
public class UserDaoImpl implements UserDao {
    private static Connection connection = null;
    private static PreparedStatement statement = null;
    private static ResultSet rs = null;

    /*
     * (non-Javadoc)
     * 
     * @see com.bailiban.dao.UserDao#findAllUser()
     */
    @Override
    public List<User> findAllUser() {
        List<User> list = new ArrayList<User>();
        try {
            connection = JDBCUtils.getConnection();
            String sql = "SELECT id,name,idCard from tb_user";
            statement = connection.prepareStatement(sql);
            rs = statement.executeQuery();
            while (rs.next()) {
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setIdCard(rs.getString("idCard"));
                user.setName(rs.getString("name"));
                list.add(user);
            }
            JDBCUtils.close(connection, statement, rs);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return list;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.bailiban.dao.UserDao#findUserById(int)
     */
    @Override
    public User findUserById(int id) {
        User user = new User();
        try {
            connection = JDBCUtils.getConnection();
            String sql = "SELECT id,name,idCard from tb_user where id=?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, id);
            rs = statement.executeQuery();
            while (rs.next()) {

                user.setId(rs.getInt("id"));
                user.setIdCard(rs.getString("idCard"));
                user.setName(rs.getString("name"));

            }
            JDBCUtils.close(connection, statement, rs);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return user;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.bailiban.dao.UserDao#findUserByidCard(java.lang.String)
     */
    @Override
    public User findUserByidCard(String idCard) {
        User user = new User();
        try {
            connection = JDBCUtils.getConnection();
            String sql = "SELECT id,name,idCard from tb_user where idCard=?";
            statement = connection.prepareStatement(sql);
            statement.setString(1, idCard);
            rs = statement.executeQuery();
            while (rs.next()) {

                user.setId(rs.getInt("id"));
                user.setIdCard(rs.getString("idCard"));
                user.setName(rs.getString("name"));

            }
            JDBCUtils.close(connection, statement, rs);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return user;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.bailiban.dao.UserDao#addUser(com.bailiban.pojo.User)
     */
    @Override
    public ResultMsg addUser(User user) {
        ResultMsg result = new ResultMsg();
        try {
            connection = JDBCUtils.getConnection();
            String sql = "INSERT INTO tb_user(NAME,idCard) VALUES(?,?)";
            statement = connection.prepareStatement(sql);
            statement.setString(1, user.getName());
            statement.setString(2, user.getIdCard());
            int update = statement.executeUpdate();
            if (update > 0) {
                result.setFlag(true);
            } else {
                result.setFlag(false);
            }
            JDBCUtils.close(connection, statement, rs);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return result;
    }

}
